class PositionController {

  def index = { redirect(action:list,params:params) }

  // the delete, save and update actions only accept POST requests
  static allowedMethods = [delete:'POST', save:'POST', update:'POST']

  def list = {
    params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
    [ positionInstanceList: Position.list( params ), positionInstanceTotal: Position.count() ]
  }

  def eventPositions = {
    def eventInstance = Event.get( params.id )

    if(!eventInstance) {
      flash.message = "Event not found with id ${params.id}"
      redirect(action:list)
    }
    Event refreshedEvent = Event.get(eventInstance.id)
    Set positionSet = refreshedEvent.positions
    if(!positionSet) {
      flash.message = "Positions not found for event"
      render(flash.message);
      return;
    }
    def positionInstanceList = positionSet.asList();
    render(view: "list", model:[ positionInstanceList: positionInstanceList, positionInstanceTotal: positionInstanceList.count() ])
  }

  def show = {
    def positionInstance = Position.get( params.id )

    if(!positionInstance) {
      flash.message = "Position not found with id ${params.id}"
      redirect(action:list)
    }
    else { return [ positionInstance : positionInstance ] }
  }

  def delete = {
    def positionInstance = Position.get( params.id )
    if(positionInstance) {
      try {
        positionInstance.delete()
        flash.message = "Position ${params.id} deleted"
        redirect(action:list)
      }
      catch(org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "Position ${params.id} could not be deleted"
        redirect(action:show,id:params.id)
      }
    }
    else {
      flash.message = "Position not found with id ${params.id}"
      redirect(action:list)
    }
  }

  def edit = {
    def positionInstance = Position.get( params.id )

    if(!positionInstance) {
      flash.message = "Position not found with id ${params.id}"
      redirect(action:list)
    }
    else {
      return [ positionInstance : positionInstance ]
    }
  }

  def update = {
    def positionInstance = Position.get( params.id )
    if(positionInstance) {
      if(params.version) {
        def version = params.version.toLong()
        if(positionInstance.version > version) {

          positionInstance.errors.rejectValue("version", "position.optimistic.locking.failure", "Another user has updated this Position while you were editing.")
          render(view:'edit',model:[positionInstance:positionInstance])
          return
        }
      }
      positionInstance.properties = params
      if(!positionInstance.hasErrors() && positionInstance.save()) {
        flash.message = "Position ${params.id} updated"
        redirect(action:show,id:positionInstance.id)
      }
      else {
        render(view:'edit',model:[positionInstance:positionInstance])
      }
    }
    else {
      flash.message = "Position not found with id ${params.id}"
      redirect(action:edit,id:params.id)
    }
  }

  def create = {
    def positionInstance = new Position()
    positionInstance.properties = params
    return ['positionInstance':positionInstance]
  }

  def save = {
    def positionInstance = new Position(params)
    positionInstance.setNoOfVolunteerOpenPosition(positionInstance.getRequiredVolunteers())
    if(!positionInstance.hasErrors() && positionInstance.save()) {
      flash.message = "Position ${positionInstance.id} created"
      redirect(action:show,id:positionInstance.id)
    }
    else {
      render(view:'create',model:[positionInstance:positionInstance])
    }
  }

  def unFilledPosition = {

    List unFilledPositionList = new ArrayList()
    Position pTemp

    Event eventInstance = Event.get( params.id )

    if(!eventInstance) {
      flash.message = "Event not found with id ${params.id}"
      redirect(action:list)
    }

    Event refreshedEvent = Event.get(eventInstance.id)
    Set positionSet = refreshedEvent.positions
    if(!positionSet) {
      flash.message = "Positions not found for event"
      render(flash.message);
      return;
    }
    List positionInstanceList = positionSet.asList()

    for(int i = 0; i< positionInstanceList.size(); i++){
        pTemp= positionInstanceList.get(i)

      if(pTemp && pTemp.getNoOfVolunteerOpenPosition() > 0 ){
         unFilledPositionList.add(pTemp);
       }
      pTemp = null
    }

    render(view: "list", model:[ positionInstanceList:unFilledPositionList, positionInstanceTotal: unFilledPositionList.count()])

  }
  


}
